package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

/**
 * 变更时选择“计费相关字段变更”操作类型后，经办人信息不可以新增
 */
@Service
public class AgentCustInfoAttrHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    private static final String TEMP_SERVICE_OFFER = "1072"; // 计费相关字段变更

    @Override
    public void init(CommonAttrVO commonAttr) {
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        String MainServiceOfferId = querySceneInstService.getSceneInst(sceneInstId).getServiceOfferId();
        // 获取页面勾选的操作类型
        List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(sceneInstId, "BUSI_CHANGE_SERVICEOFFERID");
        // 经办人信息属性
        KiteAttr handlerInfo = querySceneInstService.getCommonAttr(sceneInstId, "HandlerCustInfo");
        if (ServiceOfferConsts.SERVICE_OFFER_MODIFY.equals(MainServiceOfferId) && Objects.nonNull(handlerInfo)) {
            if (KiteListUtils.isNotEmpty(serviceOfferIdList) && serviceOfferIdList.contains(TEMP_SERVICE_OFFER)) {
                handlerInfo.setIsEdit(KeyConsts.IFTRUE_F);
                // 当已经选了经办人，点击计费相关字段操作类型
                if (KiteStringUtils.isEmpty(value) && KiteStringUtils.isNotEmpty(handlerInfo.getValue())) {
                    value = handlerInfo.getValue();
                }
            } else {
                handlerInfo.setIsEdit(KeyConsts.IFTRUE_T);
            }
            operateSceneCommonService.setCommonAttr(sceneInstId, handlerInfo);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
